package cn.qzuie.smart.module.dorm.service.buildings;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.qzuie.smart.module.dorm.controller.admin.buildings.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.buildings.BuildingsDO;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;

import cn.qzuie.smart.module.dorm.dal.mysql.buildings.BuildingsMapper;

import static cn.qzuie.smart.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.convertList;
import static cn.qzuie.smart.framework.common.util.collection.CollectionUtils.diffList;
import static cn.qzuie.smart.module.dorm.enums.ErrorCodeConstants.*;

/**
 * 栋 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BuildingsServiceImpl implements BuildingsService {

    @Resource
    private BuildingsMapper buildingsMapper;

    @Override
    public Integer createBuildings(BuildingsSaveReqVO createReqVO) {
        // 插入
        BuildingsDO buildings = BeanUtils.toBean(createReqVO, BuildingsDO.class);
        buildingsMapper.insert(buildings);

        // 返回
        return buildings.getId();
    }

    @Override
    public void updateBuildings(BuildingsSaveReqVO updateReqVO) {
        // 校验存在
        validateBuildingsExists(updateReqVO.getId());
        // 更新
        BuildingsDO updateObj = BeanUtils.toBean(updateReqVO, BuildingsDO.class);
        buildingsMapper.updateById(updateObj);
    }

    @Override
    public void deleteBuildings(Integer id) {
        // 校验存在
        validateBuildingsExists(id);
        // 删除
        buildingsMapper.deleteById(id);
    }

    @Override
        public void deleteBuildingsListByIds(List<Integer> ids) {
        // 删除
        buildingsMapper.deleteByIds(ids);
        }


    private void validateBuildingsExists(Integer id) {
        if (buildingsMapper.selectById(id) == null) {
            throw exception(BUILDINGS_NOT_EXISTS);
        }
    }

    @Override
    public BuildingsDO getBuildings(Integer id) {
        return buildingsMapper.selectById(id);
    }

    @Override
    public List<BuildingsDO> getBuildingsAll() {
        return buildingsMapper.selectList();
    }


    @Override
    public PageResult<BuildingsDO> getBuildingsPage(BuildingsPageReqVO pageReqVO) {
        return buildingsMapper.selectPage(pageReqVO);
    }

}